//
//  ClassVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-15.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "ClassVC.h"
#import "Root.h"
#import "Children.h"
#import "Tag.h"
#import "FengLeiCell.h"
#import "ChangPinCell.h"

#define BASE_URL @"http://www.shihuo.cn/app/getAllMenu?platform=ios&timestamp=1460377895&v=4.0.2&token=b75a13343500f26ab4a8b1bef2279c06"

@interface ClassVC () <UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    int _count;
    UITableViewCell *_lastCell;
    CGSize _size;
}
@property (nonatomic, strong) NSMutableArray *rootArr;
@property (nonatomic, strong) NSMutableArray *childrenArr;
@property (nonatomic, strong) NSMutableArray *tagArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ClassVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _rootArr = [[NSMutableArray alloc] init];
    _childrenArr = [[NSMutableArray alloc] init];
    _tagArr = [[NSMutableArray alloc] init];
    _count = 0;
    [self getData];
    [self.view addSubview:self.tableView];
    
}
#pragma mark -- 初始化tableView
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, Width/4, Height-64) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor grayColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _rootArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.contentView.backgroundColor = [UIColor grayColor];
    Root *root = _rootArr[indexPath.row];
    cell.textLabel.text = root.name;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.backgroundColor = [UIColor grayColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor redColor];
    
    if (_lastCell == cell) {
        cell.textLabel.textColor = [UIColor redColor];
    }else {
        cell.textLabel.textColor = [UIColor redColor];
        _lastCell.textLabel.textColor = [UIColor blackColor];
    }
    
    _count = (int)indexPath.row;
    [_collectionView reloadData];
    
    _lastCell = cell;
    
}

#pragma mark -- 初始化collectionView 
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//        layout.minimumInteritemSpacing = 10;
//        layout.minimumLineSpacing = 10;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(Width/4, 64, Width*3/4, Height-64) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [_collectionView registerClass:[FengLeiCell class] forCellWithReuseIdentifier:@"FengLeiCell"];
        [_collectionView registerClass:[ChangPinCell class] forCellWithReuseIdentifier:@"ChangPinCell"];
    }
    return _collectionView;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section == 0) {
        if ([_tagArr[_count] count] == 0) {
            return 0;
        }
        return [_tagArr[_count] count];
    }else {
        return [_childrenArr[_count] count];
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        FengLeiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FengLeiCell" forIndexPath:indexPath];
        Tag *tag = _tagArr[_count][indexPath.row];
        cell.lab.text = tag.tag;
        return cell;
    } else {
        ChangPinCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChangPinCell" forIndexPath:indexPath];
        Children *children = _childrenArr[_count][indexPath.row];
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:children.img]];
        cell.lab.text = children.name;
        
        return cell;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Tag *tag = _tagArr[_count][indexPath.row];
        CGSize size = [tag.tag sizeWithFont:[UIFont systemFontOfSize:14]];
        return CGSizeMake(size.width+10, size.height+10);
    } else {
        return CGSizeMake((Width*3/4-40)/3, Height/6);
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(Width*3/4, 30);
}

#pragma mark -- 获取请求数据
- (void)getData
{
    [MyRequest getWithURL:BASE_URL parameters:nil success:^(NSDictionary *dic) {
        NSArray *dataArr = [dic objectForKey:@"data"];
        for (NSDictionary *dic1 in dataArr) {
            NSDictionary *rootDic = [dic1 objectForKey:@"root_info"];
            Root *root = [[Root alloc] initWithDictionary:rootDic];
            [_rootArr addObject:root];
            
            NSArray *childrenArr = [dic1 objectForKey:@"children_info"];
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            for (NSDictionary *childrenDic in childrenArr) {
                Children *children = [[Children alloc] initWithDictionary:childrenDic];
                [arr addObject:children];
            }
            [_childrenArr addObject:arr];
            
            NSArray *tagArr = [dic1 objectForKey:@"tag_info"];
            NSMutableArray *arr1 = [[NSMutableArray alloc] init];
            for (NSDictionary *tagDic in tagArr) {
                Tag *tag = [[Tag alloc] initWithDictionary:tagDic];
                [arr1 addObject:tag];
            }
            [_tagArr addObject:arr1];
            NSLog(@"%lu",(unsigned long)self.tagArr.count);
        }
        [self.view addSubview:self.collectionView];
        [_tableView reloadData];
        //[_collectionView reloadData];
        
    } fail:^(NSError *error) {
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
